<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="学员姓名" prop="studentId" v-if="!dataForm.id">
      <el-select v-model="dataForm.studentId" filterable placeholder="学员姓名" @change="getStudentManageId">
          <el-option
            v-for="item in studentList"
            :key="item.id"
            :label="item.studentName"
            :value="item.studentId">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="跟进方式" prop="followMode">
      <el-select v-model="dataForm.followMode" placeholder="试听意向度">
        <el-option v-for="(item, index) in followModePtions" :label="item.label" :key="item.value" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="试听意向度" prop="listeningIntentionality">
      <el-select v-model="dataForm.listeningIntentionality" placeholder="试听意向度">
        <el-option v-for="(item, index) in listeningIntentionalityPtions" :label="item.label" :key="item.value" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="跟进说明" prop="followRemarks">
      <el-input type="textarea" v-model="dataForm.followRemarks" placeholder="请输入跟进说明"></el-input>
    </el-form-item>
    <el-form-item label="预约跟进时间" prop="createAt">
      <el-date-picker
            v-model="dataForm.createAt"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="跟进附件记录" prop="followAttachment">
      <el-upload
        drag
        :action="this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)"
        :before-upload="beforeUploadHandle"
        :on-success="successHandle"
        :file-list="fileList"
        :limit="1"
        style="text-align: center;">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只支持jpg、png、jpeg格式的图片！</div>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          studentId: '',
          studentManageId: '',
          followMode: '',
          followRemarks: '',
          followAttachment: '',
          listeningIntentionality: '',
          createAt: ''
        },
        dataRule: {
          followMode: [
            { required: true, message: '跟进方式不能为空', trigger: 'blur' }
          ],
          followRemarks: [
            { required: true, message: '跟进说明不能为空', trigger: 'blur' }
          ]
        },
        followModePtions: [{
          value: '0',
          label: '电话跟进'
        }, {
          value: '1',
          label: '微信跟进'
        }, {
          value: '2',
          label: 'QQ跟进'
        }, {
          value: '3',
          label: '邮件跟进'
        }, {
          value: '4',
          label: '面谈'
        }, {
          value: '5',
          label: '其他方式'
        }],
        listeningIntentionalityPtions: [{
          value: '0',
          label: '未标记'
        }, {
          value: '1',
          label: '无'
        }, {
          value: '2',
          label: '低'
        }, {
          value: '3',
          label: '中'
        }, {
          value: '4',
          label: '高'
        }],
        fileList: [],
        studentList: []
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/studentmanagefollow/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.studentId = data.studentManageFollow.studentId
                this.dataForm.studentManageId = data.studentManageFollow.studentManageId
                this.dataForm.followMode = data.studentManageFollow.followMode
                this.dataForm.followRemarks = data.studentManageFollow.followRemarks
                this.dataForm.followAttachment = data.studentManageFollow.followAttachment
                this.dataForm.createAt = data.studentManageFollow.createAt
                this.dataForm.listeningIntentionality = this.formatIntention(data.studentManageFollow.listeningIntentionality)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/studentmanagefollow/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'studentId': this.dataForm.studentId,
                'studentManageId': this.dataForm.studentManageId,
                'followMode': this.dataForm.followMode,
                'followRemarks': this.dataForm.followRemarks,
                'followAttachment': this.dataForm.followAttachment,
                'listeningIntentionality': this.dataForm.listeningIntentionality,
                'createAt': this.dataForm.createAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取学员
      getStudentList () {
        this.dataForm.studentId = ''
        this.$http({
          url: this.$http.adornUrl('/generator/studentmanage/getStudentList'),
          mothod: 'get',
          params: {}
        }).then(({data}) => {
          this.studentList = data.list
        })
      },
      // studentManageId
      getStudentManageId (studentId) {
        this.$http({
          url: this.$http.adornUrl('/generator/studentmanage/getStudentManageId'),
          mothod: 'get',
          params: {'studentId': studentId}
        }).then(({data}) => {
          if (data.code === 0) {
            this.dataForm.studentManageId = data.list.id
          }
        })
      },
      // 获取studentId、studentManageId
      getids (studentId, studentManageId, studentName) {
        this.visible = true
        this.dataForm.studentId = studentId
        this.dataForm.studentManageId = studentManageId
        this.studentList = []
        let list = {
          studentId: studentId,
          studentName: studentName
        }
        console.log(list)
        this.studentList.push(list)
        this.dataForm.followMode = ''
        this.dataForm.followRemarks = ''
        this.dataForm.followAttachment = ''
        this.dataForm.listeningIntentionality = ''
        this.fileList = []
      },
      // 上传之前
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png') {
          this.$message.error('只支持jpg、png、jpeg格式的图片！')
          return false
        }
      },
      // 上传成功
      successHandle (response, file, fileList) {
        if (response && response.code === 0) {
          this.dataForm.followAttachment = fileList[0].response.url
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          console.log(123)
        } else {
          this.$message.error(response.msg)
        }
      },
      // 格式化意向度
      formatIntention (item) {
        let name = null
        if (item === 0) {
          name = '未标记'
        } else if (item === 1) {
          name = '无'
        } else if (item === 2) {
          name = '低'
        } else if (item === 3) {
          name = '中'
        } else if (item === 4) {
          name = '高'
        }
        return name
      }
    }
  }
</script>
